<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="项目名称" class="filter-item" style="width: 200px" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"> 查询</el-button>
    </div>
    <!-- 数据 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column v-if="false" label="ID" prop="id" aligin="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报告名称" prop="name" width="300px"/>
      <el-table-column label="测试计划" prop="plan_name" />
      <el-table-column label="用例总数" prop="total" />
      <el-table-column label="通过数量" prop="success" />
      <el-table-column label="失败数量" prop="fail" />
      <el-table-column label="创建时间" prop="add_time">
        <template slot-scope="scope">
          <span>{{ changeDate(scope.row.add_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" prop="edit_time">
        <template slot-scope="scope">
          <span>{{ changeDate(scope.row.edit_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button type="primary" size="small" @click="handleDownload(row)">下载报告</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getReportList, getReport } from '@/api/report'
import { dateFormat } from '@/utils/data'
import { reportPath } from '@/utils/request'

export default {
  name: 'Report',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        search: '',
        page: 1,
        limit: 10
      },
      listLoading: false,
      dialogFormVisible: false,
      reportForm: {
        name: '',
        total: '',
        success: '',
        fail: '',
        plan: undefined,
        report_path: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getReportList(this.listQuery).then((res) => {
        this.listLoading = true
        this.list = res.results
        this.total = res.count
        this.listLoading = false
      })
    },
    fetchData(id) {
      getReport(id).then(response => {

      })
    },
    handleDownload(row) {
      const url = reportPath(row.id)
      window.open(url)
    },
    changeDate: function(attr) {
      return dateFormat(attr)
    },
    handleFilter() {
      console.log('goto search')
      this.total = 0
      this.listQuery.page = 1
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
